/**
 * Title:        General Utilities<p>
 * Description:  Useful general utility classes<p>
 * Check for instance of SLDataObjectRowAdaptor. Don't set null colors.
 *
 */
package com.timber.util;

import java.awt.*;
import javax.swing.*;
import pv.jfcx.*;

/**
 * JPVTable renderer for objects based on SLDataObjectRowAdaptor provides
 * methods to set renderer properties <i>setProperties<\i> set foreground,
 * background, font, formatter, etc
 */

public class DefaultSLObjectRenderer extends PVTableRenderer {
	/**
	 * @param type
	 *            one of JPVTable. constatns for cell types
	 */
	public DefaultSLObjectRenderer(int type) {
		super(type);
	}

	/**
	 * @param type
	 *            one of JPVTable. constatns for column types
	 */
	public DefaultSLObjectRenderer(int type, int alignment) {
		super(type);
		setHorizontalAlignment(alignment);
	}

	/**
	 * text cell renderer
	 */
	public DefaultSLObjectRenderer() {
		this(JPVTable.TEXT);
	}

	/**
	 * get cell renderer and allow to set properties by the extending class
	 */
	public Component getTableCellRendererComponent(JTable t, Object o,
			boolean sel, boolean foc, int row, int col) {
		SLTableModel tm = (SLTableModel) t.getModel();

		setProperties(tm, row, col, o);

		super.getTableCellRendererComponent(t, o, sel, foc, row, col);
		return this;
	}

	/**
	 * Method is called to set renderer properties table model and the data
	 * object itself are passed as arguments This method can be overloaded by
	 * extending classes to cutomize colors, fonts, etc.
	 * 
	 * @param tm
	 *            taber data model
	 * @param row
	 * @param col
	 * @param o
	 *            cell object
	 */
	public void setProperties(SLTableModel tm, int row, int col, Object o) {
		setColorsAndFont(tm, row, col);
	}

	/**
	 * Method is called to set renderer color and font properties based on
	 * <b>table row properties.<\b> table model is passed as argument This
	 * method can be overloaded by extending classes to cutomize colors & fonts
	 * 
	 * @param tm
	 *            taber data model
	 * @param row
	 * @param col
	 */
	public void setColorsAndFont(SLTableModel tm, int row, int col) {
		Object item = tm.itemAtTableRow(row);

		Color bg = null;
		Color fg = null;

		if (item != null && item instanceof SLDataObjectRowAdaptor) {
			SLDataObjectRowAdaptor ra = (SLDataObjectRowAdaptor) item;

			bg = ra.getBackground(item, tm.getColumnId(col));
			fg = ra.getForeground(item, tm.getColumnId(col));
			Font font = ra.getFont();
			setFont(font);
		} else if (item != null && item instanceof SLDataObjectAdaptor) {
			SLDataObjectAdaptor oa = (SLDataObjectAdaptor) item;
			SLColor bgc = oa.getBackgroundColorCode(item, tm.getColumnId(col));
			if ( bgc != null ){
				bg = new Color(bgc._red, bgc._green, bgc._blue);
			}
			SLColor fgc = oa.getForegroundColorCode(item, tm.getColumnId(col));
			if ( fgc != null ){
				fg = new Color(fgc._red, fgc._green, fgc._blue);
			}
			

		}

		setForeground(fg);
		setBackground(bg);

	}
}
